package com.jacky.util;

import android.content.Context;
import android.view.OrientationEventListener;

/**
 * 手机感应旋转角度
 */
public class OcrOrientationEventListener extends OrientationEventListener {

    private int orientation;
    //默认后置摄像头
    private boolean isCameraFacingBack = true;

    public OcrOrientationEventListener(Context context) {
        super(context);
    }

    public void setCameraFacingBack(boolean cameraFacingBack) {
        isCameraFacingBack = cameraFacingBack;
    }

    @Override
    public void onOrientationChanged(int orientation) {
        if(orientation == ORIENTATION_UNKNOWN) {
            // OcrLogger.w("orientations", orientation);
            return;
        }
        this.orientation = orientation;
    }

    public int getRotation() {
        int rotation;
        if(orientation > 325 || orientation <= 45){
            rotation = isCameraFacingBack ? 90 : 270;
        }else if(orientation <= 135){
            rotation = 180;
        }else if(orientation <= 225){
            rotation = isCameraFacingBack ? 270 : 90;
        }else {
            rotation = 0;
        }
//        OcrLogger.d("orientations", orientation,"use rotation",rotation);
        return rotation;
    }

}
